import axios from 'axios'
import Cookie from 'js-cookie'
import { Notify } from 'vant'
import router from '../router'
import qs from 'qs'


var CryptoJS = require("crypto-js");
import api from 'api/api'
let CancelToken = axios.CancelToken
let source = CancelToken.source()
let appKey = 6772665683285921
let appSecret = 'd7421bfb7039883652d1981cc82899836263b781'

const service = axios.create({
    baseURL: '',
    withCredentials: true,
    timeout: 30000
})

axios.defaults.headers.post['Content-Type'] = 'application/json'

service.interceptors.request.use((config) => {
    const ticket = Cookie.get('ticket')
    if (!ticket) {
        if (typeof config.requireToken === 'undefined' || config.requireToken === true) {
            config.cancelToken = source.token // 阻止请求
        }
    }
    if (config.method === 'post') {
        config.data.appKey = appKey
        // config.data = qs.stringify(config.data)
    }
    try {
        var userInfor = localStorage.getItem('userInfor')
        var accessToken = JSON.parse(userInfor).accessToken
        if (accessToken) {
            config.data.accessToken = accessToken
        }
    } catch (error) {

    }
    var orgSign = config.url + JSON.stringify(config.data) + appSecret
    config.headers.sign = CryptoJS.HmacSHA1(orgSign, appSecret).toString()
    return config
}, error => {
    Promise.reject(error)
})

service.interceptors.response.use(response => {
    return response
}, error => {
    // Notify('服务器异常');
    Promise.reject(error)
})

const fetch = (method, url, data = {}, config) => {
    config = config || {}
    config.method = method
    config.url = url
    if (method.toLowerCase() === 'get') {
        config['params'] = data
    } else {
        config['data'] = data
    }

    return service(config).then(function (res) {
        var data = res.data
        if (data.code != 200) {
            // Notify(data.msg)
        }
        var userInfor = localStorage.getItem('userInfor')
        if (userInfor) {
            userInfor = JSON.parse(userInfor)
        }
        if (data.code == 1001) {
            router.push('/login')
        }
        //     if (data.code == 1001 && userInfor.refreshToken) {
        //         api.refreshToken({
        //             data: {
        //                 refreshToken: userInfor.refreshToken
        //             }
        //         }).then(res => {
        // console.log(res)
        // return
        //             if (res.code == 200) {
        //                 localStorage.setItem('userInfor', JSON.stringify(res.data))
        //                 window.location.reload()
        //             } else {
        //                 localStorage.removeItem('userInfor')
        //                 router.push('/login')
        //             }
        //         })
        //     }

        return data
    })
}

const get = (url, data, config) => {
    return fetch('get', url, data, config)
}

const post = (url, data, config) => {
    return fetch('post', url, data, config)
}

export { get, post }
